package team.kirohuji.repository.jpa.user;

import java.io.Serializable;
import java.util.Date;

import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.ValidatingSession;
import org.apache.shiro.session.mgt.eis.CachingSessionDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import team.kirohuji.domain.user.ShiroSession;
import team.kirohuji.utils.SerializableUtils;

/**
 * 用来管理和缓存Sesion
 * 
 * @author kirohuji
 *
 */
public class MySessionDAO extends CachingSessionDAO {

	/**
	 * @see ShiroSessionDao
	 */
	@Autowired
	private ShiroSessionRepository shiroSessionRepository;

	/*
	 * 这个方法为session封装,然后存入shiroSession (non-Javadoc)
	 * 
	 * @see
	 * org.apache.shiro.session.mgt.eis.AbstractSessionDAO#doCreate(org.apache.shiro
	 * .session.Session)
	 * 
	 * @param session 会话
	 * 
	 * @return Serializable
	 */
	@Override
	protected Serializable doCreate(Session session) {

		Serializable sessionId = generateSessionId(session);

		assignSessionId(session, sessionId);

		ShiroSession shiroSession = new ShiroSession();

		shiroSession.setCreateTime(new Date());

		shiroSession.setShiroSession(SerializableUtils.serialize(session));

		shiroSession.setShiroSessionId(sessionId.toString());

		/*
		 * logger.debug(Thread.currentThread().hashCode()+"thread name ="+Thread.
		 * currentThread().getName()
		 * +"====Serializable doCreate(Session session)======> sessionId.toString()>>>>>"
		 * +sessionId.toString());
		 */

		shiroSessionRepository.save(shiroSession);

		// String sql = "insert into hzdy_shiro_session(shiro_session_id,
		// shiro_session,create_time) values(?,?,?)";

		// jdbcTemplate.update(sql, sessionId, SerializableUtils.serialize(session),new
		// Date());

		return session.getId();
	}

	/*
	 * 这个方法用来更新Session,创建一个新Session,根据主键,更新Session (non-Javadoc)
	 * 
	 * @see
	 * org.apache.shiro.session.mgt.eis.CachingSessionDAO#doUpdate(org.apache.shiro.
	 * session.Session)
	 * 
	 * @param session 会话
	 */
	@Override
	protected void doUpdate(Session session) {
		// 如果会话过期/停止 没必要再更新了
		if (session instanceof ValidatingSession && !((ValidatingSession) session).isValid()) {

			return;
		}

		ShiroSession shiroSession = new ShiroSession();

		shiroSession.setCreateTime(new Date());

		shiroSession.setShiroSession(SerializableUtils.serialize(session));

		shiroSession.setShiroSessionId(session.getId().toString());

		/*
		 * logger.debug(Thread.currentThread().hashCode()+"thread name ="+Thread.
		 * currentThread().getName()
		 * +"===void doUpdate(Session session) =====> session.getId().toString() ===="
		 * +session.getId().toString());
		 */
		shiroSessionRepository.saveAndFlush(shiroSession);

		// String sql = "update hzdy_shiro_session set shiro_session=? ,create_time=?
		// where shiro_session_id=?";

		// jdbcTemplate.update(sql, SerializableUtils.serialize(session), new
		// Date(),session.getId());
	}

	/*
	 * 这个方法用来删除session对象,shiroSessionDao根据当前给定的seesion的Id来删除 (non-Javadoc)
	 * 
	 * @see
	 * org.apache.shiro.session.mgt.eis.CachingSessionDAO#doDelete(org.apache.shiro.
	 * session.Session)
	 * 
	 * @param session 会话
	 */
	@Override
	protected void doDelete(Session session) {

		/*
		 * logger.debug(Thread.currentThread().hashCode()+"thread name ="+Thread.
		 * currentThread().getName()
		 * +"===void doDelete(Session session)=====>session.getId().toString()=="
		 * +session.getId().toString());
		 */
		shiroSessionRepository.deleteById(session.getId().toString());
		// String sql = "delete from hzdy_shiro_session where shiro_session_id=?";

		// jdbcTemplate.update(sql, session.getId());
	}

	/*
	 * 根据sessionId获取session (non-Javadoc)
	 * 
	 * @see
	 * org.apache.shiro.session.mgt.eis.AbstractSessionDAO#doReadSession(java.io.
	 * Serializable)
	 * 
	 * @param sessionId 会话主键
	 * 
	 * @return
	 */
	@Override
	@Transactional
	protected Session doReadSession(Serializable sessionId) {

		/*
		 * logger.debug(Thread.currentThread().hashCode()+"thread name ="+Thread.
		 * currentThread().getName()
		 * +"===Session doReadSession(Serializable sessionId)  =====> sessionId.toString()======="
		 * +sessionId.toString());
		 */
		System.out.println("---------------------------------------------------------------");
		ShiroSession sessionStrList = shiroSessionRepository.getOne(sessionId.toString());

		if (sessionStrList == null) {
			return null;
		}

		return SerializableUtils.deserialize(sessionStrList.getShiroSession());
	}
}
